package main

import (
	"fmt"
	"github.com/grokify/html-strip-tags-go"
	"golang.org/x/net/html"
	"net/url"
	"regexp"
	"strings"
	"log"
)
const
	IMG_REG=`<\s*ocs_j\s+[^>]*?href\s*=\s*["'](.*?)["'][^>]*?/>`
func main()  {
	//]
	urlstr:="http://tc.sinaimg.cn/maxwidth.2048/tc.service.weibo.com/p/mmbiz_qpic_cn/e53bc4eb47132a20f03eb5dc8b756cf5.jpg"
	URL,_:=url.Parse(urlstr)
	fmt.Println(URL)
	str:=`<p>121<b>2<ocs_j val="饥饿" href="face/24.gif" phrase="/::g" /><ocs_j val="饥饿" href="face/24.gif" phrase="/::g" /><ocs_j val="饥饿" href="face/24.gif" phrase="/::g" /><ocs_j val="饥饿" href="face/24.gif" phrase="/::g" /><ocs_i href="http://10.21.19.15/group1/M00/00/01/ChUTD1wTZCmAJq9BAAA4cWkMWCI754.png"/>11212</p>`
	doc, _ := html.Parse(strings.NewReader(str))
	imageReg:=regexp.MustCompile(IMG_REG)
	fmt.Println(imageReg.FindAllStringSubmatch(str,-1))
	fmt.Println(strip.StripTags(str))
	// <\s*img\s+[^>]*?src\s*=\s*(\'|\")(.*?)\\1[^>]*?\/?\s*>
	reg :=regexp.MustCompile(`<\s*ocs_j\s+[^>]*?phrase\s*=\s*["'](.*?)["'][^>]*?/>`)
	fmt.Println(reg.ReplaceAllString(str,"${1}"))
	var parse func(*html.Node)
	parse = func(n *html.Node) {
		log.Printf("node type:%v  ", n)
		if n.Type==html.TextNode{
			log.Printf("TEXT %s",n.Data)
		}

		if n.Type == html.ElementNode && n.Data == "a" {
			if n.FirstChild != nil && n.FirstChild.Type == html.TextNode {
				log.Printf("href:%s, text:%s \n", n.Attr[0].Val, n.FirstChild.Data)
			} else {
				log.Printf("href:%s \n", n.Attr[0].Val)
			}

		}

		for c := n.FirstChild; c != nil; c = c.NextSibling {
			parse(c)
		}
	}
	parse(doc)
}
